class Solution {
public:
    int getKth(int lo, int hi, int k)
    {
        vector<pair<int, int>> v;
        for (int i = lo; i <= hi; i++)
        {
            int x = i;
            int j = x;
            int cnt = 0;
            while (__builtin_popcount((unsigned int)x) != 1)
            {
                if (x & 1)
                    x = 3 * x + 1;
                else
                    x /= 2;
                cnt++;
            }
            cnt += __lg(x);
            v.push_back(make_pair(cnt, j));
        }
        ranges::sort(v);
        return v[k - 1].second;
    }
};